<?php

include_once './app/view/webView.php' ;
include_once './app/model/userModel.php' ;

class userController extends Controller
{
	
	public function home ( )
	{
		
		System::redirectTo ( ) ;
		
	}
	
	public function connection ( )
	{
		
		$validator = new KValidator ( 'connection_form', KValidator::POST ) ;
		
		$sent = $validator->sent ( ) ;
		$valid = $validator->valid ( ) ;
		
		if ( ! $sent || ! $valid )
			$this->data['text'] = 0 ;
		else
		{
			
			$this->db = &DB::getInstance ( require './config/db_config.php' ) ;
			
			$user = userModel::getByLogin ( $validator->getValue ( 'login' ) ) ;
			
			if ( is_null ( $user ) )
				$this->data['text'] = 1 ;
			else if ( $user->getPassword ( ) != $validator->getValue( 'password' ) )
				$this->data['text'] = 2 ;
			else
			{
				
				$this->data['text'] = 3 ;
				
				$this->session['user'] = $user->getId ( ) ;
				
			}
			
			unset ( $user ) ;
			
		}
		
		unset ( $validator, $sent, $valid ) ;
		
		$this->render ( 'webView', 'connection_return' ) ;
		
	}
	
	public function disconnection ( )
	{
		
		$this->session->stop ( ) ;
		
		System::redirectTo ( ) ;
		
	}
	
}

?>